<template>
  <lb-layout
    v-loading="$store.getters.loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="#a19f9fb0;"
    :paddBottom="5"
  >
    <aps-form
      :config="apsForm.formConfig"
      :model="apsForm.formData"
      :formRef="apsForm.formRef"
      :labelWidth="apsForm.labelWidth"
      ref="form"
    ></aps-form>
    <lb-table
      :column="tableData.column"
      :data="tableData.data"
      border
      :height="700"
      background
      layout="total, sizes, prev, pager, next, jumper"
    >
    </lb-table>
    <!-- <component
      :is="modelName"
      :id="id"
      :visible.sync="show"
      @close="close"
    ></component> -->
  </lb-layout>
</template>

<script>
import {
  getArea,
  getOrganization,
  getRegionDept,
} from "@/api/areaReport/index.js";
import CascaderSelect from "@/components/Select/CascaderSelect";

// import operate from "@/views/work/areaReport/components/operate.vue";
export default {
  name: "",
  components: {
    // operate,
    CascaderSelect,
  },
  dicts: ["organization_type"],
  data() {
    return {
      //弹窗
      id: "",
      modelName: "",
      show: false,
      // areaProps: {
      //   value: "areaId",
      //   children: "children",
      //   label: "name",
      //   checkStrictly: true,
      //   expandTrigger: "hover",
      // },
      areaArr: [],
      apsForm: {
        formRef: "form",
        labelWidth: "auto",
        formData: {},
        formConfig: {
          formAttr: {
            inline: true,
            size: "small",
          },
          properties: {
            year: {
              label: "年份:",
              typeName: "el-date-picker",
              type: "year",
              size: "small",
              placeholder: "选择年份",
              "value-format": "yyyy",
            },
            deptId: {
              label: "区域:",
              render: (h) => {
                let that = this;
                let arr = [...that.areaArr];
                // 数据严谨处理,处理后端数据异常情况
                let valNum = that.$refs?.form?.getFieldsValue().deptId;
                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择区域",
                        value: valNum,
                        clearable: true,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue("deptId", value);
                          that.$forceUpdate();
                        },
                      },
                    },
                    [
                      arr.map((item) => {
                        let { label, value } = item;
                        return h("el-option", {
                          props: {
                            label,
                            value: value,
                            key: value,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
              // render: (h) => {
              //   let that = this;
              //   let valNum = that.$refs?.form?.getFieldsValue().areaId;
              //   let a = (
              //     <CascaderSelect
              //       values={valNum}
              //       value={valNum}
              //       requestMethod={"patch"}
              //       varient={"zousanArea"}
              //       propOptions={that.areaProps}
              //       filterable={true}
              //       size={"small"}
              //       placeholder={"请选择区域"}
              //       onInput={(value) => {
              //         console.log(value, "areaId");
              //         if (value && value.length > 0) {
              //           this.$refs?.form?.setFieldValue(
              //             "areaId",
              //             value[value.length - 1]
              //           );
              //         }
              //       }}
              //     ></CascaderSelect>
              //   );
              //   return <div>{a}</div>;
              // },
            },
            organizationType: {
              label: "类型:",
              render: (h) => {
                let that = this;
                let arr = [];
                arr = [
                  {
                    label: "按区域",
                    value: 0,
                  },
                  {
                    label: "按机构类型",
                    value: 1,
                  },
                ];
                // 数据严谨处理,处理后端数据异常情况
                let valNum =
                  that.$refs?.form?.getFieldsValue().organizationType;
                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择查询类型",
                        value: valNum,
                        clearable: true,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue(
                            "organizationType",
                            value
                          );
                          that.setTableColumn(value);
                          that.$forceUpdate();
                        },
                      },
                    },
                    [
                      arr.map((item) => {
                        let { label, value } = item;
                        return h("el-option", {
                          props: {
                            label,
                            value: value,
                            key: value,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
            },
          },
          formSoltButton: [
            {
              label: "搜索",
              type: "primary",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-search",
              click: this.handlerSearchDetail,
            },
            {
              label: "重置",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-refresh-left",
              click: this.handleReset,
            },
          ],
        },
      },
      tableData: {
        column: [],
        data: [],
      },
    };
  },
  created() {},
  mounted() {
    this.requestRegionDept();
    this.$refs?.form?.setFieldValue("organizationType", 0);
    this.setTableColumn(0);
    setTimeout(() => {
      this.handlerSearchDetail();
    }, 200);
  },
  methods: {
    requestRegionDept() {
      this.areaArr = [];
      getRegionDept()
        .then((res) => {
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          let arr = res?.data ?? [];
          if (arr.length > 0) {
            arr.map((item) => {
              let obj = {
                label: item?.deptName ?? "未知部门",
                value: item?.deptId,
              };
              this.areaArr.push(obj);
            });
          }
        })
        .catch(() => {
          // this.$message.error("系统错误,请联系管理员!");
        });
    },
    async initData(data) {
      let that = this;
      let query = {
        ...data,
      };
      let requestWay;
      if (query.organizationType == 0) {
        requestWay = getArea;
      } else {
        requestWay = getOrganization;
      }
      if (query.year) {
        query.year = query.year + "年";
      }
      query.organizationType = undefined;
      that.$store.commit("app/updateApiCount", "add");
      await requestWay(query)
        .then((res) => {
          that.$store.commit("app/updateApiCount");
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          console.log(res);
          that.tableData.data = res?.data ?? [];
        })
        .catch((err) => {
          that.$store.commit("app/updateApiCount");
          // this.$message.error("系统错误,请联系管理员!");
        });
    },
    // 搜索
    handlerSearchDetail() {
      const { ...other } = this.$refs?.form?.getFieldsValue();
      const data = {
        ...other,
      };
      this.initData(data);
    },
    // 重置
    handleReset() {
      this.$refs?.form?.resetFields();
      this.$refs?.form?.setFieldValue("organizationType", 0);
      this.$refs?.form?.setFieldValue("deptId", undefined);
      this.setTableColumn(0);
      this.handlerSearchDetail();
    },
    typeJudege(value) {
      let result;
      if (value == "0") {
        result = "未上报";
      } else if (value == "1") {
        result = "已上报";
      }
      return result;
    },
    setTableColumn(type) {
      switch (type) {
        case 0:
          this.tableData.column = [
            {
              prop: "areaName",
              label: "区域",
              align: "center",
              showOverflowTooltip: true,
            },
            {
              prop: "janLedgerReportStatus",
              label: "一月",
              align: "center",
              showOverflowTooltip: true,
              render: (h, scope) => {
                let that = this;
                let a = that.typeJudege(scope.row.janLedgerReportStatus ?? "0");
                let type = "";
                switch (a) {
                  case "未上报":
                    type = "danger";
                    break;
                  case "已上报":
                    type = "success";
                    break;
                }
                let b = (
                  <el-button type={type} plain>
                    {a}
                  </el-button>
                );
                return <div>{b}</div>;
              },
            },
            {
              prop: "febLedgerReportStatus",
              label: "二月",
              align: "center",
              showOverflowTooltip: true,
              render: (h, scope) => {
                let that = this;
                let a = that.typeJudege(scope.row.febLedgerReportStatus ?? "0");
                let type = "";
                switch (a) {
                  case "未上报":
                    type = "danger";
                    break;
                  case "已上报":
                    type = "success";
                    break;
                }
                let b = (
                  <el-button type={type} plain>
                    {a}
                  </el-button>
                );
                return <div>{b}</div>;
              },
            },
            {
              prop: "marLedgerReportStatus",
              label: "三月",
              align: "center",
              showOverflowTooltip: true,
              render: (h, scope) => {
                let that = this;
                let a = that.typeJudege(scope.row.marLedgerReportStatus ?? "0");
                let type = "";
                switch (a) {
                  case "未上报":
                    type = "danger";
                    break;
                  case "已上报":
                    type = "success";
                    break;
                }
                let b = (
                  <el-button type={type} plain>
                    {a}
                  </el-button>
                );
                return <div>{b}</div>;
              },
            },
            {
              prop: "aprLedgerReportStatus",
              label: "四月",
              align: "center",
              showOverflowTooltip: true,
              render: (h, scope) => {
                let that = this;
                let a = that.typeJudege(scope.row.aprLedgerReportStatus ?? "0");
                let type = "";
                switch (a) {
                  case "未上报":
                    type = "danger";
                    break;
                  case "已上报":
                    type = "success";
                    break;
                }
                let b = (
                  <el-button type={type} plain>
                    {a}
                  </el-button>
                );
                return <div>{b}</div>;
              },
            },
            {
              prop: "mayLedgerReportStatus",
              label: "五月",
              align: "center",
              showOverflowTooltip: true,
              render: (h, scope) => {
                let that = this;
                let a = that.typeJudege(scope.row.mayLedgerReportStatus ?? "0");
                let type = "";
                switch (a) {
                  case "未上报":
                    type = "danger";
                    break;
                  case "已上报":
                    type = "success";
                    break;
                }
                let b = (
                  <el-button type={type} plain>
                    {a}
                  </el-button>
                );
                return <div>{b}</div>;
              },
            },
            {
              prop: "junLedgerReportStatus",
              label: "六月",
              align: "center",
              showOverflowTooltip: true,
              render: (h, scope) => {
                let that = this;
                let a = that.typeJudege(scope.row.junLedgerReportStatus ?? "0");
                let type = "";
                switch (a) {
                  case "未上报":
                    type = "danger";
                    break;
                  case "已上报":
                    type = "success";
                    break;
                }
                let b = (
                  <el-button type={type} plain>
                    {a}
                  </el-button>
                );
                return <div>{b}</div>;
              },
            },
            {
              prop: "julLedgerReportStatus",
              label: "七月",
              align: "center",
              showOverflowTooltip: true,
              render: (h, scope) => {
                let that = this;
                let a = that.typeJudege(scope.row.julLedgerReportStatus ?? "0");
                let type = "";
                switch (a) {
                  case "未上报":
                    type = "danger";
                    break;
                  case "已上报":
                    type = "success";
                    break;
                }
                let b = (
                  <el-button type={type} plain>
                    {a}
                  </el-button>
                );
                return <div>{b}</div>;
              },
            },
            {
              prop: "augLedgerReportStatus",
              label: "八月",
              align: "center",
              showOverflowTooltip: true,
              render: (h, scope) => {
                let that = this;
                let a = that.typeJudege(scope.row.augLedgerReportStatus ?? "0");
                let type = "";
                switch (a) {
                  case "未上报":
                    type = "danger";
                    break;
                  case "已上报":
                    type = "success";
                    break;
                }
                let b = (
                  <el-button type={type} plain>
                    {a}
                  </el-button>
                );
                return <div>{b}</div>;
              },
            },
            {
              prop: "sepLedgerReportStatus",
              label: "九月",
              align: "center",
              showOverflowTooltip: true,
              render: (h, scope) => {
                let that = this;
                let a = that.typeJudege(scope.row.sepLedgerReportStatus ?? "0");
                let type = "";
                switch (a) {
                  case "未上报":
                    type = "danger";
                    break;
                  case "已上报":
                    type = "success";
                    break;
                }
                let b = (
                  <el-button type={type} plain>
                    {a}
                  </el-button>
                );
                return <div>{b}</div>;
              },
            },
            {
              prop: "octLedgerReportStatus",
              label: "十月",
              align: "center",
              showOverflowTooltip: true,
              render: (h, scope) => {
                let that = this;
                let a = that.typeJudege(scope.row.octLedgerReportStatus ?? "0");
                let type = "";
                switch (a) {
                  case "未上报":
                    type = "danger";
                    break;
                  case "已上报":
                    type = "success";
                    break;
                }
                let b = (
                  <el-button type={type} plain>
                    {a}
                  </el-button>
                );
                return <div>{b}</div>;
              },
            },
            {
              prop: "novLedgerReportStatus",
              label: "十一月",
              align: "center",
              showOverflowTooltip: true,
              render: (h, scope) => {
                let that = this;
                let a = that.typeJudege(scope.row.novLedgerReportStatus ?? "0");
                let type = "";
                switch (a) {
                  case "未上报":
                    type = "danger";
                    break;
                  case "已上报":
                    type = "success";
                    break;
                }
                let b = (
                  <el-button type={type} plain>
                    {a}
                  </el-button>
                );
                return <div>{b}</div>;
              },
            },
            {
              prop: "decLedgerReportStatus",
              label: "十二月",
              align: "center",
              showOverflowTooltip: true,
              render: (h, scope) => {
                let that = this;
                let a = that.typeJudege(scope.row.decLedgerReportStatus ?? "0");
                let type = "";
                switch (a) {
                  case "未上报":
                    type = "danger";
                    break;
                  case "已上报":
                    type = "success";
                    break;
                }
                let b = (
                  <el-button type={type} plain>
                    {a}
                  </el-button>
                );
                return <div>{b}</div>;
              },
            },
            {
              prop: "completionRate",
              label: "本年完成率",
              align: "center",
              showOverflowTooltip: true,
            },
          ];
          break;
        case 1:
          this.tableData.column = [
            {
              prop: "organizationType",
              label: "机构类型",
              align: "center",
              showOverflowTooltip: true,
              render: (h, scope) => {
                return (
                  <div>
                    <dict-tag
                      options={this.dict.type.organization_type}
                      value={scope.row.organizationType}
                    />
                  </div>
                );
              },
            },
            {
              prop: "janLedgerReportRatio",
              label: "一月",
              align: "center",
              showOverflowTooltip: true,
            },
            {
              prop: "febLedgerReportRatio",
              label: "二月",
              align: "center",
              showOverflowTooltip: true,
            },
            {
              prop: "marLedgerReportRatio",
              label: "三月",
              align: "center",
              showOverflowTooltip: true,
            },
            {
              prop: "aprLedgerReportRatio",
              label: "四月",
              align: "center",
              showOverflowTooltip: true,
            },
            {
              prop: "mayLedgerReportRatio",
              label: "五月",
              align: "center",
              showOverflowTooltip: true,
            },
            {
              prop: "junLedgerReportRatio",
              label: "六月",
              align: "center",
              showOverflowTooltip: true,
            },
            {
              prop: "julLedgerReportRatio",
              label: "七月",
              align: "center",
              showOverflowTooltip: true,
            },
            {
              prop: "augLedgerReportRatio",
              label: "八月",
              align: "center",
              showOverflowTooltip: true,
            },
            {
              prop: "sepLedgerReportRatio",
              label: "九月",
              align: "center",
              showOverflowTooltip: true,
            },
            {
              prop: "octLedgerReportRatio",
              label: "十月",
              align: "center",
              showOverflowTooltip: true,
            },
            {
              prop: "novLedgerReportRatio",
              label: "十一月",
              align: "center",
              showOverflowTooltip: true,
            },
            {
              prop: "decLedgerReportRatio",
              label: "十二月",
              align: "center",
              showOverflowTooltip: true,
            },
          ];
          break;
      }
      this.tableData.data = [];
      this.handlerSearchDetail();
    },
    // handleSizeChange(val) {
    //   this.currentPage = 1;
    //   this.pageSize = val;
    //   this.handlerSearchDetail();
    // },
    // handleCurrentChange(val) {
    //   this.currentPage = val;
    //   this.handlerSearchDetail();
    // },
    // 弹窗
    // showModel(name, params) {
    //   this.modelName = name;
    //   this.id = params?.id || "";
    //   setTimeout(() => {
    //     this.show = true;
    //   }, 200);
    // },
    // close(status) {
    //   this.show = false;
    //   setTimeout(() => {
    //     if (status) this.handlerSearchDetail();
    //   }, 200);
    // },
  },
};
</script>

<style lang="scss" scoped></style>
